import { isArray, isBoolean, isEmpty, isFunction, isString, isUndefined, log } from "@/utils"
import { DisabledProps, PermissionProps, VisibleProps } from "./types"
import { useAuthStore } from "@/store/modules/auth"

export * from "./types"

/**
 * 判断是否有权限访问
 */
export function hasPermission(props: PermissionProps): boolean {
    const { permissions } = props
    if (isEmpty(permissions)) {
        return true
    }
    const authStore = useAuthStore()
    if (isString(permissions)) {
        return authStore.hasPermission(permissions as string)
    } else if (isArray(permissions)) {
        return authStore.hasPermissions(permissions as string[])
    }
    log.error("权限配置格式错误：" + permissions)
    return false
}

/**
 * 判断是否有权限访问
 */
export function isShow(props: VisibleProps, defaultValue: boolean = true, ...args: any[]): boolean {
    const { show } = props
    if (isUndefined(show)) {
        return defaultValue
    }
    if (isBoolean(show)) {
        return show as boolean
    }
    if (isFunction(show)) {
        return (show as Function)(...args)
    }
    log.error("可见性格式错误：" + show)
    return false
}

/**
 * 判断是否有权限访问
 */
export function isDisabled(props: DisabledProps, defaultValue: boolean = false, ...args: any[]): boolean {
    const { disabled } = props
    if (isUndefined(disabled)) {
        return defaultValue
    }
    if (isBoolean(disabled)) {
        return disabled as boolean
    }
    if (isFunction(disabled)) {
        return (disabled as Function)(...args)
    }
    log.error("可见性格式错误：" + disabled)
    return false
}
